package org.jeecg.modules.music.controller;

import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CacheConstant;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.music.entity.MusCategory;
import org.jeecg.modules.music.model.CategoryIdModel;
import org.jeecg.modules.music.model.MusCategoryTreeModel;
import org.jeecg.modules.music.service.IMusCategoryService;
import org.jeecg.modules.system.entity.SysDepart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

@Slf4j
@Api(tags="分类维护")
@RestController
@RequestMapping("/mus/category")
public class MusCategoryController {

    @Autowired
    private IMusCategoryService musCategoryService;

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result<MusCategory> add(@RequestBody MusCategory category, HttpServletRequest request) {
        Result<MusCategory> result = new Result<MusCategory>();
        String username = JwtUtil.getUserNameByToken(request);
        try {
            category.setCreateBy(username);
            musCategoryService.saveCategoryData(category, username);
            //清除部门树内存
            // FindsDepartsChildrenUtil.clearMusCategoryTreeList();
            // FindsDepartsChildrenUtil.clearCategoryIdModel();
            result.success("添加成功！");
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            result.error500("操作失败");
        }
        return result;
    }

    @RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
    public Result<MusCategory> edit(@RequestBody MusCategory MusCategory, HttpServletRequest request) {
        String username = JwtUtil.getUserNameByToken(request);
        MusCategory.setUpdateBy(username);
        Result<MusCategory> result = new Result<MusCategory>();
        MusCategory MusCategoryEntity = musCategoryService.getById(MusCategory.getId());
        if (MusCategoryEntity == null) {
            result.error500("未找到对应实体");
        } else {
            boolean ok = musCategoryService.updateCategoryDataById(MusCategory, username);
            // TODO 返回false说明什么？
            if (ok) {
                //清除部门树内存
                //FindsDepartsChildrenUtil.clearMusCategoryTreeList();
                //FindsDepartsChildrenUtil.clearDepartIdModel();
                result.success("修改成功!");
            }
        }
        return result;
    }

    /**
     * 查询数据 查出所有部门,并以树结构数据格式响应给前端
     *
     * @return
     */
    @RequestMapping(value = "/queryTreeList", method = RequestMethod.GET)
    public Result<List<MusCategoryTreeModel>> queryTreeList(@RequestParam(name = "ids", required = false) String ids) {
        Result<List<MusCategoryTreeModel>> result = new Result<>();
        try {
            // 从内存中读取
//			List<MusCategoryTreeModel> list =FindsDepartsChildrenUtil.getMusCategoryTreeList();
//			if (CollectionUtils.isEmpty(list)) {
//				list = MusCategoryService.queryTreeList();
//			}
            if(oConvertUtils.isNotEmpty(ids)){
                List<MusCategoryTreeModel> departList = musCategoryService.queryTreeList(ids);
                result.setResult(departList);
            }else{
                List<MusCategoryTreeModel> list = musCategoryService.queryTreeList();
                result.setResult(list);
            }
            result.setSuccess(true);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return result;
    }

    /**
     * 查询数据 添加或编辑页面对该方法发起请求,以树结构形式加载所有部门的名称,方便用户的操作
     *
     * @return
     */
    @RequestMapping(value = "/queryIdTree", method = RequestMethod.GET)
    public Result<List<CategoryIdModel>> queryIdTree() {
        Result<List<CategoryIdModel>> result = new Result<>();
        try {
            List<CategoryIdModel> list = musCategoryService.queryCategoryIdTreeList();
            result.setResult(list);
            result.setSuccess(true);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return result;
    }

    @RequestMapping(value = "/searchBy", method = RequestMethod.GET)
    public Result<List<MusCategoryTreeModel>> searchBy(@RequestParam(name = "keyWord", required = true) String keyWord) {
        Result<List<MusCategoryTreeModel>> result = new Result<List<MusCategoryTreeModel>>();
        List<MusCategoryTreeModel> treeList = this.musCategoryService.searchByKeyWord(keyWord);
        if (treeList == null || treeList.size() == 0) {
            result.setSuccess(false);
            result.setMessage("未查询匹配数据！");
            return result;
        }
        result.setResult(treeList);
        return result;
    }

    @RequestMapping(value = "/deleteBatch", method = RequestMethod.DELETE)
    public Result<MusCategory> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {

        Result<MusCategory> result = new Result<MusCategory>();
        if (ids == null || "".equals(ids.trim())) {
            result.error500("参数不识别！");
        } else {
            this.musCategoryService.deleteBatchWithChildren(Arrays.asList(ids.split(",")));
            result.success("删除成功!");
        }
        return result;
    }

    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public Result<MusCategory> delete(@RequestParam(name="id",required=true) String id) {

        Result<MusCategory> result = new Result<MusCategory>();
        MusCategory sysDepart = musCategoryService.getById(id);
        if(sysDepart==null) {
            result.error500("未找到对应实体");
        }else {
            boolean ok = musCategoryService.delete(id);
            if(ok) {
                //清除部门树内存
                //FindsDepartsChildrenUtil.clearSysDepartTreeList();
                // FindsDepartsChildrenUtil.clearDepartIdModel();
                result.success("删除成功!");
            }
        }
        return result;
    }
}
